package com.qdu.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.qdu.bean.Log;
import com.qdu.bean.Pic;
import com.qdu.bean.User;
import com.qdu.commons.AccessToken;
import com.qdu.util.EncryptUtil;
import com.qdu.commons.Result;
import com.qdu.service.LogService;
import com.qdu.service.PersonService;
import com.qdu.service.PicService;
import com.qdu.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/picc")
@Slf4j
@RestController
public class PicController {
    @Autowired
    PersonService personService;

    @Autowired
    LogService logService;

    @Autowired
    PicService picService;


    @RequestMapping("/getLog")
    public Result<List> getLog(HttpServletRequest request){
        String token = request.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = personService.getUser(username);
        List<Log> logList = personService.getLog(user.getCompany());
        return Result.success(logList);
    }

    @RequestMapping("/handlepass")
    public Result<String> handlePass(String name,String id,HttpServletRequest request){
        String token = request.getHeader("token");
        String username = JWTUtil.getUsername(token);

        User user = personService.getUser(username);
        picService.handlePass(id);
        String include = name + "图纸已经通过核验";
        Log log = new Log();
        log.setUsername(user.getName());
        log.setCompany(user.getCompany());
        log.setInclude(include);
        logService.doneLog(log);
        return Result.success("通过！");
    }


    @RequestMapping("/unpass")
    public Result<String> unPass(String id,String include,HttpServletRequest request){
        String token = request.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = personService.getUser(username);
        Log log = new Log();
        log.setInclude(include);
        log.setUsername(user.getName());
        picService.deletePic(id);
        logService.doneLog(log);

        return Result.success("您的记录已提交");


    }

    @RequestMapping("/showpic")
    public Result<PageInfo> showPic(Integer pageNumber,Integer pageSize, HttpServletRequest request){
        String token = request.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = personService.getUser(username);
        PageInfo<Pic> picPageInfo = picService.queryPic(user.getCompany(), user.getPower(), pageNumber,pageSize);
        return Result.success(picPageInfo);
    }


    @RequestMapping("/addpic")
    public Result<String> addPic(MultipartFile file){
        if(file.getSize() <= 0) {
            return Result.failed("请上传文件");
        }

        /*
         * 创建新文件名称
         */
        String filename = file.getOriginalFilename();
        String suffix = FilenameUtils.getExtension(filename);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String newFilename = uuid + "." + suffix;

        // 创建上传文件夹
        File uploadDirectory = new File("d:/upload");
        if(!uploadDirectory.exists()) {
            uploadDirectory.mkdirs();
        }

        // 创建目标文件，并上传
        File destFile = new File(uploadDirectory, newFilename);
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.failed("服务器出现异常，请重新上传");
        }
        String path = "/pic/" + newFilename;
        log.info("文件路径 - {}", path);
        Pic pic = new Pic();
        pic.setSavepath(path);
        Pic pic1 = picService.addPic(pic);
        return Result.success(pic1.getId()+"");
    }

    @RequestMapping("/donepic")
    public Result<String> doneAdd(@RequestBody Pic pic, HttpServletRequest request){
        String token = request.getHeader("token");
        String username = JWTUtil.getUsername(token);
        User user = personService.getUser(username);
        pic.setCompany(user.getCompany());
        picService.updatePic(pic);
        return Result.success("添加成功");
    }

}
